'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class TransferWindowViewModel
    Implements System.ComponentModel.INotifyPropertyChanged

    Public Sub New()
        Dim svConnector As IcqInterface.Interfaces.IConnector
        Dim svTransfer As IcqInterface.IIcqDataTranferService

        svConnector = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IConnector)()
        svTransfer = DirectCast(svConnector, IcqInterface.IIcqDataTranferService)

        AddHandler svConnector.Disconnected, AddressOf OnSignOut
        AddHandler svTransfer.FlapReceived, AddressOf OnFlapReceived
        AddHandler svTransfer.FlapSent, AddressOf OnFlapSent
    End Sub

    Private Sub OnSignOut(ByVal sender As Object, ByVal e As IcqInterface.Interfaces.DisconnectedEventArgs)
        Try
            Message += String.Format("{0}: *** DISCONNECTED (Exp: {1}) *** {2}", Date.Now.ToLongTimeString, e.IsExpected, e.Message) & System.Environment.NewLine
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnFlapReceived(ByVal sender As Object, ByVal e As IcqInterface.FlapTransportEventArgs)
        Dim infos As List(Of String)

        Try
            infos = New List(Of String)

            For Each x As IcqInterface.DataTypes.ISerializable In e.Flap.DataItems
                infos.Add(DirectCast(x, Object).ToString)
            Next

            If infos.Count > 0 Then
                Message += String.Format("{0}: << Seq: {1} Channel: {2} Items: {3}", Date.Now.ToLongTimeString, e.Flap.DatagramSequenceNumber, e.Flap.Channel, String.Join(", ", infos.ToArray)) & System.Environment.NewLine
            Else
                Message += String.Format("{0}: << Seq: {1} Channel: {2}", Date.Now.ToLongTimeString, e.Flap.DatagramSequenceNumber, e.Flap.Channel) & System.Environment.NewLine
            End If
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnFlapSent(ByVal sender As Object, ByVal e As IcqInterface.FlapTransportEventArgs)
        Dim infos As List(Of String)

        Try
            infos = New List(Of String)

            For Each x As IcqInterface.DataTypes.ISerializable In e.Flap.DataItems
                infos.Add(DirectCast(x, Object).ToString)
            Next

            Message += String.Format("{0}: >> Seq: {1} Channel: {2} Items: {3}", Date.Now.ToLongTimeString, e.Flap.DatagramSequenceNumber, e.Flap.Channel, String.Join(", ", infos.ToArray)) & System.Environment.NewLine
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private _Message As String
    Public Property Message() As String
        Get
            Return _Message
        End Get
        Set(ByVal value As String)
            _Message = value

            OnPropertyChanged("Message")
        End Set
    End Property

    Private Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
